package org.ellisande.grid.tags;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Required;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Defines a dropdown list inside a navList. This tag expects navListItems or list items (li) in the body.")
public class ListDropdownTag extends AbstractTag {

	private static final long serialVersionUID = -8913849779877648316L;

	@Attribute
	@Required
	@Description("Defines the title of the dropdown list.")
	private String title;

	@Override
	public void startTag() throws Exception {
		TagBuilder listItem = createTagBuilder("li");
		TagBuilder anchor = createTagBuilder("a");
		TagBuilder icon = createTagBuilder("i");
		TagBuilder list = createTagBuilder("ul");

		listItem.addStyle("dropdown");
		anchor.addStyle("dropdown-toggle");
		anchor.addAttribute("data-toggle", "dropdown");
		anchor.addAttribute("href", "#");
		icon.addStyle("caret");
		list.addStyle("dropdown-menu");

		JspWriter out = pageContext.getOut();

		out.println(listItem.compileStartTag());
		out.println(anchor.compileStartTag());
		out.println(title);
		out.println(icon.compileStartTag() + icon.compileEndTag());
		out.println(anchor.compileEndTag());
		out.println(list.compileStartTag());
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder listItem = createTagBuilder("li");
		TagBuilder list = createTagBuilder("ul");
		JspWriter out = pageContext.getOut();
		out.println(list.compileEndTag());
		out.print(listItem.compileEndTag());
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public void release() {
		super.release();
		title = null; 
	}
}
